package com.yourname.myapplication.presentation.activity

import MyAppTheme
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.yourname.myapplication.data.repository.QuestionRepository
import com.yourname.myapplication.presentation.screen.quiz.QuizScreen
import com.yourname.myapplication.presentation.screen.quiz.QuizViewModel
import kotlinx.coroutines.*

class MainActivity : ComponentActivity() {
    private val viewModel by viewModels<QuizViewModel> {
        val db = AppDatabase.create(this)
        QuizViewModelFactory(QuestionRepository(db.questionDao()))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 初始化测试数据
        lifecycleScope.launch {
            viewModel.repository.initSampleData()
        }

        setContent {
            MyAppTheme {
                QuizScreen(viewModel)
            }
        }
    }
}

// ViewModel工厂
class QuizViewModelFactory(private val repository: QuestionRepository) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        return QuizViewModel(repository) as T
    }
}